<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LpnADConnect {
		public function getEmcodeByUsername($username){
			$dn_base = 'DC=lpn,DC=co,DC=th';
			$dn_host = '172.16.1.32';
			$ldapusers = 'Ldapmapper@lpn.co.th';
			$ldappasswd = 'P@ssw0rd!@#$%^';
			$search=$username;
			$ldapconn = ldap_connect($dn_host , 389) or die("Could not connect to LDAP Server.");
			ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
			ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
			$ldapbind = ldap_bind($ldapconn ,$ldapusers, $ldappasswd) or die ("Users or Password Invalid.");
			$filter = "(sAMAccountName=$search)"; // CN = Common Name, SN = Surname , OU = Organization Unit
			$result = ldap_search($ldapconn,$dn_base,$filter);
			$entries = ldap_count_entries($ldapconn,$result );
			$data= array();
			$data['error']=0;
			if( $entries == 0){
				$data['error']=1;
			}
			$info = ldap_get_entries($ldapconn, $result );
			return $info[0]['description'][0];
		}
		
public function getADByUsername($username){
			$dn_base = 'DC=lpn,DC=co,DC=th';
			$dn_host = '172.16.1.32';
			$ldapusers = 'Ldapmapper@lpn.co.th';
			$ldappasswd = 'P@ssw0rd!@#$%^';
			$search=$username;
			$ldapconn = ldap_connect($dn_host , 389) or die("Could not connect to LDAP Server.");
			ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
			ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
			$ldapbind = ldap_bind($ldapconn ,$ldapusers, $ldappasswd) or die ("Users or Password Invalid.");
			// Search
			$filter = "(sAMAccountName=$search)"; // CN = Common Name, SN = Surname , OU = Organization Unit
			$result = ldap_search($ldapconn,$dn_base,$filter);
			$entries = ldap_count_entries($ldapconn,$result );
			$data= array();
			$data['error']=0;
			if( $entries == 0){
				$data['error']=1;
			}
			$info = ldap_get_entries($ldapconn, $result );
			$data['email']=$info[0]['mail'][0];
			$data['mobile']=$info[0]['mobile'][0];
			$data['EM_TNAME']=split(" ",$info[0]['displayname'][0]);
			$data['EORG_04']=$info[0]['department'][0];
			$data['comp']=$info[0]['physicaldeliveryofficename'][0];
			$data['tel']=$info[0]['telephonenumber'][0];
			$data['EM_CODE']=$info[0]['description'][0]; // รหัสพนักงาน
			$data['eorg_03']=$info[0]['physicaldeliveryofficename'][0]; // สายงาน
			$data['eorg_04']=$info[0]['department'][0]; // ฝ่าย
			$data['op_tname']=$info[0]['title'][0]; // ตำแหน่ง
			$data['nickname']=$info[0]['initials'][0]; // ตำแหน่ง
			return $data;
}
}